package com.viphuli.app.tool.handler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;

import com.offroader.utils.DateUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.page.ArrangeViewWeekPage;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.bean.part.ArrangeView;
import com.viphuli.app.tool.bean.part.ArrangeViewDay;
import com.viphuli.app.tool.fragment.ArrangeRecordDetailArrangeFragment;

public class ArrangeRecordDetailArrangeWeekViewResponseHandler extends
		MyBaseHttpResponseHandler<ArrangeRecordDetailArrangeFragment, ArrangeViewWeekPage> {

	@Override
	public void deal() {

		long time = page.time();

		//初始周视图
		initViewWeekHeader(time);
		initViewWeekRows(page.getArrangeViewList());

	}

	private void initViewWeekHeader(long time) {

		//创建头
		ViewGroup header = ViewUtils.inflateView(caller.getActivity(), R.layout.include_week_view_header);

		//设置头
		Date date = new Date(time);
		List<Date> dateList = DateUtils.dateToWeek(date);

		for (int i = 0; i < dateList.size(); i++) {

			View curDayView = header.getChildAt(i + 1);//路过空白

			TextView nameText = ButterKnife.findById(curDayView, R.id.id_view_week_header_name);
			TextView dateText = ButterKnife.findById(curDayView, R.id.id_view_week_header_date);

			Date curDate = dateList.get(i);

			String[] weekDays = caller.getResources().getStringArray(R.array.week_days);
			String name = DateUtils.getWeekOfDate(curDate, weekDays);
			SimpleDateFormat dayFormat = new SimpleDateFormat("MM-dd");
			String dateStr = dayFormat.format(curDate);

			//设置周日期
			nameText.setText(name);
			dateText.setText(dateStr);

		}

		//添加头
		caller.getViewWeekHeaderLayout().removeAllViews();
		caller.getViewWeekHeaderLayout().addView(header);
	}

	private void initViewWeekRows(List<ArrangeView> arrangeViewList) {
		
		//每个人的周排班信息
		for (ArrangeView arrangeView : arrangeViewList) {

			//每个人的排班数据 
			AccountUser user = arrangeView.getUser();//人
			List<ArrangeViewDay> arrangeViewDayList = arrangeView.getArrangeDayList();//一周的排班信息

			ViewGroup row = ViewUtils.inflateView(caller.getActivity(), R.layout.include_week_view);
			caller.getViewWeekLayout().addView(row);

			for (int i = 0; i < row.getChildCount(); i++) {

				View rowItem = row.getChildAt(i);

				if (i == 0) {//护士名

					TextView rowItemText = ButterKnife.findById(rowItem, R.id.id_view_week_row_item_text);
					rowItemText.setText(user.getUserName());

				} else {//星期日、星期一、星期二、星期三、星期四、星期五、星期六
					
					//每天的排班信息
					ArrangeViewDay arrangeViewDay = arrangeViewDayList.get(i - 1);

					//每天有多个排班
					List<ArrangeType> arrangeTypeList = arrangeViewDay.getArrangeTypeList();

					//文本组件
					View rowItemText = ButterKnife.findById(rowItem, R.id.id_view_week_row_item_text);

					//设置排班信息
					if (arrangeTypeList != null && !arrangeTypeList.isEmpty()) {

						List<String> names = new ArrayList<String>(2);
						List<String> colors = new ArrayList<String>(2);

						for (int j = 0; j < arrangeTypeList.size(); j++) {

							ArrangeType arrangeType = arrangeTypeList.get(j);
							names.add(arrangeType.getName());
							colors.add(arrangeType.getColor());

						}

						ViewGroup rowItemTextLayout = (ViewGroup) rowItemText;
						if (arrangeTypeList.size() == 1) {

							TextView text = (TextView) rowItemTextLayout.getChildAt(0);
							text.setTextColor(Color.parseColor(colors.get(0)));
							text.setText(names.get(0));

						} else if (arrangeTypeList.size() == 2) {

							TextView textOne = (TextView) rowItemTextLayout.getChildAt(0);
							TextView textTwo = (TextView) rowItemTextLayout.getChildAt(1);

							textTwo.setVisibility(View.VISIBLE);

							textOne.setTextColor(Color.parseColor(colors.get(0)));
							textOne.setText(names.get(0));

							textTwo.setTextColor(Color.parseColor(colors.get(1)));
							textTwo.setText(names.get(1));
						}

					}

				}

			}

		}

	}
}
